package org.jboss.seam.example.registration;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.FlushModeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.faces.FacesMessages;

@Name("administrableUsersManager")
@Scope(ScopeType.CONVERSATION)
public class AdministrableUsersManager implements Serializable {
	
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 9159312504839014257L;


	@In("userDatabase")
	private EntityManager em;
	
	
	private List<DatatableItem<User>> administrableUsers;
	
	@DataModelSelection
	private DatatableItem<User> userItem;
	
//	@In
//	private DatatableItem<User> datatableItem;
	
	@Begin(flushMode = FlushModeType.MANUAL,join = true) 
	@SuppressWarnings("unchecked")
	@Factory
	@DataModel(scope = ScopeType.PAGE)
	public List<DatatableItem<User>> getAdministrableUsers() {
		if (administrableUsers == null) {
			administrableUsers = new ArrayList<DatatableItem<User>>();
			List<User> users = em
					.createQuery(
							"select user from User user where upper(user.roleExpr) not like '%ADMIN%'")
					.getResultList();
			for (User user : users) {
				DatatableItem<User> datatableItem = new DatatableItem<User>();
				datatableItem.setEntity(user);
				administrableUsers.add(datatableItem);
			}
		}
		return administrableUsers;
	}
	
	public void setAdministrableUsers(List<DatatableItem<User>> administrableUsers) {
		this.administrableUsers = administrableUsers;
	}
	
	@Observer("userAdd")
	public void addUser() {
		User user = new User("change it","12345","change it");
		em.persist(user);
		DatatableItem<User> userItem = new DatatableItem<User>();
		userItem.setEntity(user);
		getAdministrableUsers().add(userItem);
		FacesMessages.instance().add("Added a user.");
	}

	/**
	 * 
	 */
	@Observer("saveUsers")
//	@End
	public void saveUsers() {
		em.flush();
		FacesMessages.instance().add("Saved changes made to users.");
	}
	
	@Observer("userDelete")
//	@End
	public void deleteUser() {
		getAdministrableUsers().remove(userItem);
		em.remove(userItem.getEntity());
		em.flush();
		FacesMessages.instance().add("Deleted user: #0",userItem.getEntity().getUsername());
	}
	
//	@End
	@Observer("deleteUsers")
	public void deleteUsers() {
		for (Iterator<DatatableItem<User>> userItemIter = getAdministrableUsers().iterator();userItemIter.hasNext();) {
			DatatableItem<User> userItem = userItemIter.next();
			if (userItem.isChecked()) {
				userItemIter.remove();
				em.remove(userItem.getEntity());
			}
		}
		em.flush();
		FacesMessages.instance().add("Deteled checked users.");
	}
	
//	@Destroy
//	public void destroy() {
//		if (administrableUsers != null) {
//			if (!administrableUsers.isEmpty()) {
//				administrableUsers.clear();
//			}
//			administrableUsers = null;
//		}
//	}

}
